<template>
  <div class="order-wrapper">
    <div class="search-container">
      <div class="order-num-container search-item">
        <div class="title">联系电话：</div>
        <el-input v-model="phone" type="text" placeholder="请输入联系电话" class="input-container" />
      </div>
      <el-button type="primary" class="search-button" @click="handleSearch">搜索</el-button>
    </div>
    <el-table ref="multipleTable" v-loading="loading" :data="tableData" border tooltip-effect="dark" style="width: 100%">
      <el-table-column prop="id" label="编号" width="200" />
      <el-table-column prop="contact_phone" label="联系电话" width="150" />
      <el-table-column prop="package_name" label="兑换房型" width="150" />
      <el-table-column prop="exchange_day" label="天数" width="150" />
      <el-table-column prop="contact_name" label="起止时间" width="400">
        <template slot-scope="scope">
          <div>{{ scope.row.start_at.substr(0, 10) }} 至 {{ scope.row.end_at.substr(0, 10) }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="success">已兑换</el-tag>
          <el-tag v-if="scope.row.status === 1">已到店</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button type="primary" @click="handledetail(scope)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>listQuery.limit" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { roomList } from '@/api/order'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'
export default {
  name: 'OrderList',
  components: {
    Pagination
  },
  props: {
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      loading: false,
      total: 0,
      phone: '',
      listQuery: {
        page: 1,
        limit: 10
      },
      list: []
    }
  },
  computed: {
    tableData() {
      return this.list.map((item) => {
        item.start_at = item.start_at ? parseTime(item.start_at) : ''
        item.end_at = item.end_at ? parseTime(item.end_at) : ''
        return item
      })
    }
  },
  created() {
    this.initList()
  },
  methods: {
    // 分页
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.initList()
    },
    async initList() {
      this.loading = true
      const { phone } = this
      const { page, limit } = this.listQuery
      const res = await roomList({ phone, page, limit })
      this.loading = false
      const { total, lists } = res.data
      this.total = total
      this.list = lists
    },
    handleSearch() {
      this.initList()
    },
    handledetail(scope) {
      const { id } = scope.row
      this.$router.push({
        path: `/order/roomTime/roomTimeDetails`,
        query: {
          id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.order-wrapper {
  width: 100%;
  height: 100%;
  .search-container{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 20px;
    .search-item {
      display: flex;
      align-items: center;
      margin-right: 20px;
      .title {
        width: 80px;
      }
      .input-container{
        width: 300px;
      }
    }
  }
}
</style>
